using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;

namespace ShippingRemoteClient
{
    public class TCPController
    {
        private TcpClient client;
        private RemoteTerminal terminal;

        public TCPController()
        {
            client = new TcpClient();
            terminal = new RemoteTerminal();
        }

        public TCPController(RemoteTerminal term)
        {
            client = new TcpClient();
            terminal = term;
        }

        public RemoteTerminal Terminal
        {
            get
            {
                return terminal;
            }
            set
            {
                terminal = value;
            }
        }

        public void Connect(string host, string uname, string upass)
        {
            byte[] name = Encoding.Unicode.GetBytes(uname);
            byte[] pass = Encoding.Unicode.GetBytes(upass);
            byte[] response = new byte[15];
            string response_string = "";
            try
            {
                terminal.SendLog("Connecting to " + host + "...");
                //if (client.Client == null)
                //{
                //    client.Connect(host, 8001);
                //}
                client.Client.Connect(host, 8001);
                terminal.SendLog("Connected to " + host);
                client.Client.Send(name);
                client.Client.Receive(response);
                client.Client.Send(pass);
                terminal.SendLog("Logging in...\nUsername: " + uname + "\nPassword: " + upass);
                client.Client.Receive(response);
                response_string = CleanString(Encoding.Unicode.GetString(response));
                if (response_string == "OK")
                {
                    terminal.RecieveLog("Accepted. Logged in. Server responded with " + response_string);
                }
                else
                {
                    terminal.RecieveLog("Rejected. Server responded with " + response_string);
                }
                
            }
            catch(Exception e)
            {
                terminal.RecieveLog("There was a connection error! The server said: " + e.Message);
            }
        }

        public void Ping()
        {
            byte[] response = new byte[8];
            try
            {
                DateTime first = new DateTime();
                client.Client.Send(Encoding.Unicode.GetBytes("PING"));
                client.Client.Receive(response);
                DateTime second = new DateTime();
                TimeSpan time = second - first;
                terminal.RecieveLog("Ping: " + time.TotalMilliseconds.ToString() + " ms");
            }
            catch
            {
                terminal.RecieveLog("Error or no response to Ping");
            }
        }

        public string RequestList()
        {
            byte[] size = new byte[8];
            byte[] list;
            try
            {
                client.Client.Send(Encoding.Unicode.GetBytes("REQUESTLIST"));
                terminal.SendLog("Command: REQUEST LIST");
                client.Client.Receive(size);
                list = new byte[int.Parse(Encoding.Unicode.GetString(size)) * 2];
                client.Client.Send(Encoding.Unicode.GetBytes("OK"));
                client.Client.Receive(list);
                terminal.RecieveLog(CleanString(Encoding.Unicode.GetString(list)));
                return CleanString(Encoding.Unicode.GetString(list));
            }
            catch
            {
                terminal.RecieveLog("Error or no packages");
                return "";
            }
        }

        public void Logoff()
        {
            byte[] response = new byte[6];
            try
            {
                client.Client.Send(Encoding.Unicode.GetBytes("LOGOFF"));
                client.Client.Receive(response);
                client.Close();
                terminal.RecieveLog("Disconnected. Server responded with: " + Encoding.Unicode.GetString(response));
            }
            catch
            {
                terminal.RecieveLog("Error or no response to Disconnect");
            }
        }

        public string CleanString(string s)
        {
            string temp = "";
            foreach (char c in s)
            {
                if (c > 32 && c < 128)
                {
                    temp += c;
                }
            }
            return temp;
        }
    }
}
